﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Runda.CMS.Common.Entity;
using Runda.CMS.Bll;

namespace Runda.CMS.Portal.ContractManage
{
    public partial class ItemGuarantyAdd : BasePage
    {
        public override bool IsCheckAccessible
        {
            get
            {
                return false;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["ItemID"] != null && Request.QueryString["ItemID"] != "")
                {
                    if (Request.QueryString["GuarantyID"] != null)
                    {
                        hiddenGuaranty.Value = new Guid(Request.QueryString["GuarantyID"]).ToString();
                        InitForm(hiddenGuaranty.Value);
                    }
                    else
                    {
                        txtstartDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                        CtrtContractItemBll bll = new CtrtContractItemBll();
                        CtrtContractItemEntity entity = bll.Get<CtrtContractItemEntity>(new Guid(Request.QueryString["ItemID"]));
                        if (entity != null)
                        { 
                            if(entity.GuaranteeYear != 0)
                            {
                                txtendDate.Text = DateTime.Now.AddYears(entity.GuaranteeYear).ToString("yyyy-MM-dd");
                            }
                        }
                    }
                    hiddenItem.Value = Request.QueryString["ItemID"];
                }
            }
        }
        private void InitForm(String guarantyID)
        {
            CtrtItemGuarantyEntity entity = new CtrtItemGuarantyEntity();
            CtrtItemGuarantyBll bll = new CtrtItemGuarantyBll();

            if (guarantyID != String.Empty)
            {
                entity = bll.Get<CtrtItemGuarantyEntity>(new Guid(guarantyID));
                listguarantyStatus.SelectedValue = entity.guarantyStatus.ToString();
                listguarantyType.SelectedValue = entity.guarantyType.ToString();
                txtstartDate.Text = entity.startDate.ToString();
                txtendDate.Text = entity.endDate.ToString();              
            }
        }
        protected void lnkbtnSave_Click(object sender, EventArgs e)
        {
            string Message = "";
            if (!Save(ref Message))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "saveandcloseerror", "alert('" + Message + "');", true);
            }
            else
                ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('保存成功');", true);
        }

        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e)
        {
            string Message = "";
            if (!Save(ref Message))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "saveandssscloseerror", "alert('" + Message + "');", true);
            }
            else
                ClientScript.RegisterStartupScript(this.GetType(), "saveandclose", "window.close();", true);
        }

        protected void lnkbtnSaveAndNew_Click(object sender, EventArgs e)
        {
            string Message = "";
            if (!Save(ref Message))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "saveansrror", "alert('" + Message + "');", true);
            }
            else
            {
                txtstartDate.Text = "";
                txtendDate.Text = "";
                hiddenGuaranty.Value = "";
                ClientScript.RegisterStartupScript(this.GetType(), "saveandnew", "alert('保存成功');", true);
            }
        }

        private bool Save(ref string Message)
        {
            if (hiddenItem.Value.Trim() == "")
            {
                Message = "参数传递错误!请联系管理员";
                return false;
            }
            CtrtItemGuarantyEntity entity = new CtrtItemGuarantyEntity();
            CtrtItemGuarantyBll bll = new CtrtItemGuarantyBll();

            if (hiddenGuaranty.Value.Trim() != "")
            {
                entity = bll.Get<CtrtItemGuarantyEntity>(new Guid(hiddenGuaranty.Value));
            }
            else
            {
                entity = new CtrtItemGuarantyEntity();
                entity.GuarantyID = Guid.NewGuid();
            }

            entity.ItemID = new Guid(hiddenItem.Value.Trim());
            entity.guarantyStatus = System.Convert.ToInt32(listguarantyStatus.SelectedValue);
            entity.guarantyType = System.Convert.ToInt32(listguarantyType.SelectedValue);
            if (txtstartDate.Text.Trim() != "")
            {
                entity.startDate = System.Convert.ToDateTime(txtstartDate.Text.Trim());
            }
            if (txtendDate.Text.Trim() != "")
            {
                entity.endDate = System.Convert.ToDateTime(txtendDate.Text.Trim());
            }
            bll.InsertOrUpdate(entity);
            return true;
        }
    }
}
